package fragment;

import java.util.ArrayList;
import java.util.List;

import com.android.volley.VolleyError;
import com.qzhy.female.R;

import Constant.Constance;
import activity.CircleDetailActivity;
import adapter.CircleAdapter;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
import model.Category;
import model.Circle_status;
import model.FocusCircle_Result;
import model.FocusInfo;
import model.FocusStatus;
import utils.GsonUtils;
import utils.HTTPUtils;
import utils.SetUtils;
import volley.VolleyListener;
import widget.MyListView;
import widget.MyListView.onRefreshListener;

/**
 * A simple {@link Fragment} subclass.
 *
 */
public class PostFragment extends BaseFragment {

	private MyListView mPrListview;
	private CircleAdapter circlrAdapter;
    private ArrayList<Category>  DateCircleInfo=new ArrayList<Category>();
    private ArrayList<FocusInfo> mFocunInfo=new ArrayList<FocusInfo>();
	public PostFragment() {
		// Required empty public constructor
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view=inflater.inflate(R.layout.fragment_post, null);
		GetDate();
		getFocusCircle();
		InitUI(view);
		return view;
	}
	private void getFocusCircle() {
		HTTPUtils.get(getActivity(), Constance.URL.USER+"/"+SetUtils.GetUserId(getActivity())+"/focus", new VolleyListener() {
			@Override
			public void onResponse(String arg0) {
//				Toast.makeText(getApplicationContext(), arg0.toString(),1).show();
				FocusStatus focus = GsonUtils.parseJSON(arg0, FocusStatus.class);
				if(focus.getStatus().equals(Constance.STATUS_TYPE.SUCCESS)){
					mFocunInfo.clear();
					FocusCircle_Result result = focus.getResult();
					mFocunInfo.addAll(result.getFocus());
					circlrAdapter.notifyDataSetChanged();
					Log.e("mFocunInfo容器", ""+mFocunInfo.size());
				}
			}
			
			@Override
			public void onErrorResponse(VolleyError arg0) {
				
			}
		});
	}

	private void GetDate() {
//		showLoadingDialog();
		getCircleInfo();		
	}

	private void getCircleInfo() {
		Log.i("getCircleInfo", "getCircleInfo");
		
		HTTPUtils.get(getContext(), Constance.URL.USERCIRCLE, new VolleyListener() {
			@Override
			public void onResponse(String arg0) {
				Circle_status circle_status = GsonUtils.parseJSON(arg0, Circle_status.class);
				String status = circle_status.getStatus();
				if(status.equals(Constance.STATUS_TYPE.SUCCESS)){
					dismissLoadingDialog();
					dismissLoadingDialog();
					List<Category> circlr_info = circle_status.getResult();
					DateCircleInfo.clear();
					DateCircleInfo.addAll(circlr_info);
					circlrAdapter.notifyDataSetChanged();
					Log.e("DateCircleInfo容器", ""+DateCircleInfo.size());
				}else {
					showProgressDialog();
				}
			}
			@Override
			public void onErrorResponse(VolleyError arg0) {
				Toast.makeText(getContext(), "VolleyError", Toast.LENGTH_SHORT).show();
				showProgressDialog();
			}
		});
	}

	private void InitUI(View view) {
		mPrListview = (MyListView) view.findViewById(R.id.ptrListView);
		circlrAdapter = new CircleAdapter(getActivity(),DateCircleInfo,mFocunInfo);
		mPrListview.setAdapter(circlrAdapter);
		mPrListview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(getActivity(),CircleDetailActivity.class);
				SetUtils.saveCircleID(getContext(), DateCircleInfo.get(position-1).getId());
				startActivity(intent);
			}
		});
		mPrListview.setOnRefreshListener(new onRefreshListener() {
			
			@Override
			public void refresh() {
				new GetDataTask().execute();
			}
			@Override
			public void loadMore() {
				mPrListview.onRefreshComplete();
			}
		});
}
	private class GetDataTask extends AsyncTask<Void, Void, String> {
		protected String doInBackground(Void... params) {
			try {
				Thread.sleep(3000);
				getCircleInfo();
			} catch (InterruptedException e) {
			}
			return "";
		}

		protected void onPostExecute(String result) {
//			mGoodList.addAll(0, result);
//			mAdapter.notifyDataSetChanged();
				// 关闭正在加载动画布局
			mPrListview.onRefreshComplete();
		}
	}
	@Override
	public void onDestroyView() {
//		dismissLoadingDialog();
		super.onDestroyView();
	}
}
